<?php
class systemActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        if ($this->getUser()->getAttribute("adminUserName") == null || ($this->getUser()->getAttribute("adminUserName") != null && $this->getUser()->getAttribute("adminUserName") != "admin")) {
            $this->redirect("system/login");
        }
        if ($request->getParameter("frameFlag") != null) {
            switch ($request->getParameter("frameFlag"))
            {
                case "top":
                    return $this->renderPartial("system/topMenud");
                    break;
                case "left":
                    return $this->renderPartial("system/leftMenud");
                    break;
                case "default":
                    return $this->renderPartial("system/defaultd");
                    break;
            }
        }
    }

    public function executeTopMenu()
    {

    }

    public function executeDefault()
    {

    }

    /*
    * 内容管理菜单
    */
    public function executeContentMenu()
    {
        sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url'));
        if ($this->getUser()->getAttribute("adminUserName") == null || ($this->getUser()->getAttribute("adminUserName") != null && $this->getUser()->getAttribute("adminUserName") != "admin")) {
            return $this->renderText(utility::getJavascript("window.top.location='/backend.php/system/login';")); //$this->redirect("system/login");
        }
        $catalogs = Doctrine_Core::getTable("Catalog")
                ->createQuery()
                ->where("is_column_b=?", "1")
                ->orderBy("catalog_id asc")
                ->execute();


        $html = "<table border='0' cellspacing='0' cellpadding='0'>";
        $i = 1;
        foreach ($catalogs as $catalog)
        {
            if ($catalog->getCatalogId() == 1004) //松糕团
                continue;
            if ($i == $catalogs->count()) {
                $html = $html . "<tr><td class='menu3' id=\"td" . $i . "\" onclick=\"onTdClick('catalog" . $i . "','td" . $i . "',3)\"><span onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $catalog->getDescription() . "</span></td></tr>";
                $html = $html . "<tr id='catalog" . $i . "' style=\"display:none;\"><td class=\"list1\">";
            }
            else
            {
                $html = $html . "<tr><td class='menu1' id=\"td" . $i . "\" onclick=\"onTdClick('catalog" . $i . "','td" . $i . "')\"><span onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $catalog->getDescription() . "</span></td></tr>";
                $html = $html . "<tr id='catalog" . $i . "' style=\"display:none;\"><td class=\"list\">";
            }
            $html = $html . $this->getContentHtml($catalog->getCatalogId(), $catalogs->count());
            $html = $html . "</td></tr>";
            $i++;
        }

        //松糕团
        $html = $html . "<tr><td class='menu1' id=\"td" . $i . "\" onclick=\"onTdClick('catalog" . $i . "','td" . $i . "')\"><span onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $catalog->getDescription() . "</span></td></tr>";
        $html = $html . "<tr id='catalog" . $i . "' style=\"display:none;\"><td class=\"list\">";
        $html = $html . "<table border='0' cellspacing='0' cellpadding='0'>";
        $html = $html . "<tr><td id='content_vote' class='file'><a href='/backend.php/celebrityVote' target=\"mainFrame\"><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>明星投票</span></a></td></tr>";
        $html = $html . "<tr><td id='content_pk' class='file'><a href='/backend.php/celebrityPk' target=\"mainFrame\"><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>明星PK</span></a></td></tr>";
        $html = $html . "<tr><td id='content_survey' class='file1'><a href='/backend.php/survey' target='mainFrame'><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>在线调查</span></a></td></tr>";
        $html = $html . "<tr><td id='question' class='file1'><a href='" . url_for('question/index') . "' target=\"mainFrame\"><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>问题征集</span></a></td></tr>";
        $i++;
        // 读者调查
        $html = $html . "<tr><td class='menu1' id=\"td" . $i . "\" onclick=\"onTdClick('catalog" . $i . "','td" . $i . "')\"><span onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">读者调查</span></td></tr>";
        $html = $html . "<tr id='catalog" . $i . "' style=\"display:none;\"><td class=\"list\">";
        $html = $html . "<table border='0' cellspacing='0' cellpadding='0'>";
        $html = $html . "<tr><td id='reader_survey' class='file1'><a href='" . url_for('readerSurvey/index') . "' target='mainFrame'><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>读者调查</span></a></td></tr>";
        $html = $html . "<tr><td id='reader_survey_question' class='file1'><a href='" . url_for('readerSurveyQuestion/index') . "' target=\"mainFrame\"><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>读者调查问题管理</span></a></td></tr>";
        $html = $html . "<tr><td id='reader_survey_feedback' class='file1'><a href='" . url_for('surveyFeedback/index') . "' target=\"mainFrame\"><span class='menuSpan' onmouseout='this.style.fontWeight=''' onmouseover='this.style.fontWeight='bold''>浏览调查表</span></a></td></tr>";
        $html = $html . "</table>";
        $html = $html . "</td></tr>";

        $html = $html . "</table>";
        $html = $html . "</td></tr>";


        $this->html = $html . "</table>";
    }

    /*
    * 功能列表菜单
    */
    public function executeFunctionMenu()
    {
        if ($this->getUser()->getAttribute("adminUserName") == null || ($this->getUser()->getAttribute("adminUserName") != null && $this->getUser()->getAttribute("adminUserName") != "admin")) {
            return $this->redirect("system/login");
        }
    }

    /*
     * 用户列表菜单
    */
    public function executeUserMenu()
    {
        if ($this->getUser()->getAttribute("adminUserName") == null || ($this->getUser()->getAttribute("adminUserName") != null && $this->getUser()->getAttribute("adminUserName") != "admin")) {
            return $this->redirect("system/login");
        }
    }

    public function executeCatalogMenu()
    {
        if ($this->getUser()->getAttribute("adminUserName") == null || ($this->getUser()->getAttribute("adminUserName") != null && $this->getUser()->getAttribute("adminUserName") != "admin")) {
            return $this->redirect("system/login");
        }
    }

    public function executeSystemMenu()
    {
        if ($this->getUser()->getAttribute("adminUserName") == null || ($this->getUser()->getAttribute("adminUserName") != null && $this->getUser()->getAttribute("adminUserName") != "admin")) {
            return $this->redirect("system/login");
        }
    }

    public function getContentHtml($catalogId, $max)
    {
        $contentTypes = Doctrine_Core::getTable("ContentType")
                ->createQuery()
                ->where("content_type_id != ?", 1004) //1004-----magzine
                ->andWhere("content_type_id != ?", 1005) //1005---Music
                ->orderBy("content_type_id asc")
                ->execute();
        $html = "<table border='0' cellspacing='0' cellpadding='0'>";
        $i = 1;
        foreach ($contentTypes as $contentType)
        {
            $indexUrl = "";
            switch ($contentType->getContentTypeId())
            {
                case 1000: //news
                    $indexUrl = "/backend.php/news/index?contentTypeId=1000&catalogId=" . $catalogId;
                    break;
                case 1001: //video
                    $indexUrl = "/backend.php/video/index?contentTypeId=1001&catalogId=" . $catalogId;
                    break;
                case 1002: //Celebrity
                    $indexUrl = "/backend.php/celebrity/index?contentTypeId=1002&catalogId=" . $catalogId;
                    break;
                case 1003: //Photo Album
                    $indexUrl = "/backend.php/album/index?contentTypeId=1003&catalogId=" . $catalogId;
                    break;
                case 1006: //video group
                    $indexUrl = "/backend.php/videoGroup"; ///index?contentTypeId=1003&catalogId=".$catalodId;
                    break;
            }
            $catalogs = $this->getDataRows("select catalog_id, description, name from catalog where catalog_id in(select catalog_id from CatalogContentType where content_type_id=" . $contentType->getContent_type_id() . ")");
            if (count($catalogs) > 0) {
                $contentId = "content" . $i . $catalogId;
                $html = $html . "<tr><td class='menu1' id='" . $contentId . "' onclick=\"onTdClick('catalog" . $i . $catalogId . "','" . $contentId . "')\"><a href=\"" . $indexUrl . "\" target=\"mainFrame\"><span class='menuSpan' onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $contentType->getDescription() . "</span></a></td></tr>";
                $html = $html . "<tr id='catalog" . $i . $catalogId . "' style=\"display:none;\"><td class=\"list\">";
                $html = $html . "<table border='0' cellspacing='0' cellpadding='0'>";
                $s = 1;
                foreach ($catalogs as $catalog)
                {
                    if ($s == count($catalogs))
                        $html = $html . "<tr><td id='content" . $i . $catalogId . "' class=\"file1\"><a href=\"" . $indexUrl . "&subCatalogId=" . $catalog["catalog_id"] . "\" target=\"mainFrame\"><span class='menuSpan' onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $catalog["description"] . "</span></a></td></tr>";
                    else
                        $html = $html . "<tr><td id='content" . $i . $catalogId . "' class=\"file\"><a href=\"" . $indexUrl . "&subCatalogId=" . $catalog["catalog_id"] . "\" target=\"mainFrame\"><span class='menuSpan' onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $catalog["description"] . "</span></a></td></tr>";
                    $s++;
                }
                $html = $html . "</table></td></tr>";
            }
            else
            {
                if ($i == $contentTypes->count())
                    $html = $html . "<tr><td id='content" . $i . $catalogId . "' class=\"file1\"><a href=\"" . $indexUrl . "\" target=\"mainFrame\"><span class='menuSpan' onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $contentType->getDescription() . "</span></a></td></tr>";
                else
                    $html = $html . "<tr><td id='content" . $i . $catalogId . "' class=\"file\"><a href=\"" . $indexUrl . "\" target=\"mainFrame\"><span class='menuSpan' onmouseout=\"this.style.fontWeight=''\" onmouseover=\"this.style.fontWeight='bold'\">" . $contentType->getDescription() . "</span></a></td></tr>";
            }

            $i++;
        }
        $html = $html . "</table>";
        return $html;
    }

    public function executeLogin(sfWebRequest $request)
    {
    }

    function getDataRows($sqlStr)
    {
        $dbBase = new DbBase();
        return $dbBase->getDataQuery($sqlStr);
    }

}
